﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using BangleCMS.DBUtility;//Please add references
namespace BangleCMS.DAL
{
	/// <summary>
	/// 数据访问类:sys_menuDAL
	/// </summary>
	public partial class sys_menuDAL
	{
		public sys_menuDAL()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperMySQL.GetMaxID("MenuId", "sys_menu"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int MenuId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from sys_menu");
			strSql.Append(" where MenuId=@MenuId");
			MySqlParameter[] parameters = {
					new MySqlParameter("@MenuId", MySqlDbType.Int32)
			};
			parameters[0].Value = MenuId;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(BangleCMS.Model.sys_menu model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into sys_menu(");
			strSql.Append("MenuName,MenuCode,href,ModularId,OrderNo,CreateUser,CreateDate,UpdateUser,UpdateDate,Valid)");
			strSql.Append(" values (");
			strSql.Append("@MenuName,@MenuCode,@href,@ModularId,@OrderNo,@CreateUser,@CreateDate,@UpdateUser,@UpdateDate,@Valid)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@MenuName", MySqlDbType.VarChar,100),
					new MySqlParameter("@MenuCode", MySqlDbType.VarChar,50),
					new MySqlParameter("@href", MySqlDbType.VarChar,200),
					new MySqlParameter("@ModularId", MySqlDbType.Int32,11),
					new MySqlParameter("@OrderNo", MySqlDbType.Int32,11),
					new MySqlParameter("@CreateUser", MySqlDbType.Int32,11),
					new MySqlParameter("@CreateDate", MySqlDbType.DateTime),
					new MySqlParameter("@UpdateUser", MySqlDbType.Int32,11),
					new MySqlParameter("@UpdateDate", MySqlDbType.DateTime),
					new MySqlParameter("@Valid", MySqlDbType.Int32,11)};
			parameters[0].Value = model.MenuName;
			parameters[1].Value = model.MenuCode;
			parameters[2].Value = model.href;
			parameters[3].Value = model.ModularId;
			parameters[4].Value = model.OrderNo;
			parameters[5].Value = model.CreateUser;
			parameters[6].Value = model.CreateDate;
			parameters[7].Value = model.UpdateUser;
			parameters[8].Value = model.UpdateDate;
			parameters[9].Value = model.Valid;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(BangleCMS.Model.sys_menu model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update sys_menu set ");
			strSql.Append("MenuName=@MenuName,");
			strSql.Append("MenuCode=@MenuCode,");
			strSql.Append("href=@href,");
			strSql.Append("ModularId=@ModularId,");
			strSql.Append("OrderNo=@OrderNo,");
			strSql.Append("CreateUser=@CreateUser,");
			strSql.Append("CreateDate=@CreateDate,");
			strSql.Append("UpdateUser=@UpdateUser,");
			strSql.Append("UpdateDate=@UpdateDate,");
			strSql.Append("Valid=@Valid");
			strSql.Append(" where MenuId=@MenuId");
			MySqlParameter[] parameters = {
					new MySqlParameter("@MenuName", MySqlDbType.VarChar,100),
					new MySqlParameter("@MenuCode", MySqlDbType.VarChar,50),
					new MySqlParameter("@href", MySqlDbType.VarChar,200),
					new MySqlParameter("@ModularId", MySqlDbType.Int32,11),
					new MySqlParameter("@OrderNo", MySqlDbType.Int32,11),
					new MySqlParameter("@CreateUser", MySqlDbType.Int32,11),
					new MySqlParameter("@CreateDate", MySqlDbType.DateTime),
					new MySqlParameter("@UpdateUser", MySqlDbType.Int32,11),
					new MySqlParameter("@UpdateDate", MySqlDbType.DateTime),
					new MySqlParameter("@Valid", MySqlDbType.Int32,11),
					new MySqlParameter("@MenuId", MySqlDbType.Int32,11)};
			parameters[0].Value = model.MenuName;
			parameters[1].Value = model.MenuCode;
			parameters[2].Value = model.href;
			parameters[3].Value = model.ModularId;
			parameters[4].Value = model.OrderNo;
			parameters[5].Value = model.CreateUser;
			parameters[6].Value = model.CreateDate;
			parameters[7].Value = model.UpdateUser;
			parameters[8].Value = model.UpdateDate;
			parameters[9].Value = model.Valid;
			parameters[10].Value = model.MenuId;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int MenuId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sys_menu ");
			strSql.Append(" where MenuId=@MenuId");
			MySqlParameter[] parameters = {
					new MySqlParameter("@MenuId", MySqlDbType.Int32)
			};
			parameters[0].Value = MenuId;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string MenuIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sys_menu ");
			strSql.Append(" where MenuId in ("+MenuIdlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BangleCMS.Model.sys_menu GetModel(int MenuId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select MenuId,MenuName,MenuCode,href,ModularId,OrderNo,CreateUser,CreateDate,UpdateUser,UpdateDate,Valid from sys_menu ");
			strSql.Append(" where MenuId=@MenuId");
			MySqlParameter[] parameters = {
					new MySqlParameter("@MenuId", MySqlDbType.Int32)
			};
			parameters[0].Value = MenuId;

			BangleCMS.Model.sys_menu model=new BangleCMS.Model.sys_menu();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["MenuId"]!=null && ds.Tables[0].Rows[0]["MenuId"].ToString()!="")
				{
					model.MenuId=int.Parse(ds.Tables[0].Rows[0]["MenuId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["MenuName"]!=null && ds.Tables[0].Rows[0]["MenuName"].ToString()!="")
				{
					model.MenuName=ds.Tables[0].Rows[0]["MenuName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["MenuCode"]!=null && ds.Tables[0].Rows[0]["MenuCode"].ToString()!="")
				{
					model.MenuCode=ds.Tables[0].Rows[0]["MenuCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["href"]!=null && ds.Tables[0].Rows[0]["href"].ToString()!="")
				{
					model.href=ds.Tables[0].Rows[0]["href"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ModularId"]!=null && ds.Tables[0].Rows[0]["ModularId"].ToString()!="")
				{
					model.ModularId=int.Parse(ds.Tables[0].Rows[0]["ModularId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["OrderNo"]!=null && ds.Tables[0].Rows[0]["OrderNo"].ToString()!="")
				{
					model.OrderNo=int.Parse(ds.Tables[0].Rows[0]["OrderNo"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateUser"]!=null && ds.Tables[0].Rows[0]["CreateUser"].ToString()!="")
				{
					model.CreateUser=int.Parse(ds.Tables[0].Rows[0]["CreateUser"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateDate"]!=null && ds.Tables[0].Rows[0]["CreateDate"].ToString()!="")
				{
					model.CreateDate=DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UpdateUser"]!=null && ds.Tables[0].Rows[0]["UpdateUser"].ToString()!="")
				{
					model.UpdateUser=int.Parse(ds.Tables[0].Rows[0]["UpdateUser"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UpdateDate"]!=null && ds.Tables[0].Rows[0]["UpdateDate"].ToString()!="")
				{
					model.UpdateDate=DateTime.Parse(ds.Tables[0].Rows[0]["UpdateDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Valid"]!=null && ds.Tables[0].Rows[0]["Valid"].ToString()!="")
				{
					model.Valid=int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select MenuId,MenuName,MenuCode,href,ModularId,OrderNo,CreateUser,CreateDate,UpdateUser,UpdateDate,Valid ");
			strSql.Append(" FROM sys_menu ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM sys_menu ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.MenuId desc");
			}
			strSql.Append(")AS Row, T.*  from sys_menu T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "sys_menu";
			parameters[1].Value = "MenuId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

